import { loadingMgr } from "./LoadingMgr";
import { logMgr } from "./LogMgr";

export const GAME_HTTP_ERROR_TYPE = {
    REQUEST_ERROR: "REQUEST_ERROR",
    REQUEST_TIMEOUT: "REQUEST_TIMEOUT"
}

export class HttpMgr {

    public static getRequest(url: string, token: string, successcb: Function, failcb: Function) {
        let request = new XMLHttpRequest();
        request.timeout = 12000;
        request.open("GET", url);
        if (token) {
            request.setRequestHeader("Authorization", token);
        }
        request.setRequestHeader("Content-Type", "application/json");
        request.onreadystatechange = function () {
            if (request.readyState == 4) {
                loadingMgr.hideLoading()
                if (request.status == 200) {
                    var responseText = request.responseText;
                    logMgr.log(`http get success: ${url} ${responseText}`)
                    request.abort()
                    request = null
                    let json = JSON.parse(responseText)
                    if (json.success) {
                        // serverTimeMgr.updateServerTime(Number(json.severTime) * 1000)
                        successcb && successcb(json)
                    } else {
                        failcb && failcb(json.msg || responseText)
                    }
                } else {
                    failcb && failcb(GAME_HTTP_ERROR_TYPE.REQUEST_ERROR)
                }
            }
        }
        request.onerror = function () {
            loadingMgr.hideLoading()
            request.abort()
            request = null
            failcb && failcb(GAME_HTTP_ERROR_TYPE.REQUEST_ERROR);
        }
        request.ontimeout = function () {
            loadingMgr.hideLoading()
            request.abort()
            request = null
            failcb && failcb(GAME_HTTP_ERROR_TYPE.REQUEST_TIMEOUT);
        }

        loadingMgr.showLoading()
        logMgr.log(`http get request: ${url}`)
        request.send()
    }

    public static postRequest(url: string, token: string, param: any, successcb: Function, failcb: Function) {
        let request = new XMLHttpRequest();
        request.timeout = 12000;
        request.open("POST", url);
        if (token) {
            request.setRequestHeader("Authorization", token);
        }
        request.setRequestHeader("Content-Type", "application/json");
        request.onreadystatechange = function () {
            if (request.readyState == 4) {
                loadingMgr.hideLoading()
                if (request.status == 200) {
                    var responseText = request.responseText;
                    logMgr.log(`http post success: ${url} ${responseText}`)
                    request.abort()
                    request = null
                    let json = JSON.parse(responseText)
                    if (json.success) {
                        // serverTimeMgr.updateServerTime(Number(json.severTime) * 1000)
                        successcb && successcb(json)
                    } else {
                        failcb && failcb(json.msg || responseText)
                    }
                } else {
                    failcb && failcb(GAME_HTTP_ERROR_TYPE.REQUEST_ERROR)
                }
            }
        }
        request.onerror = function () {
            loadingMgr.hideLoading()
            request.abort()
            request = null
            failcb && failcb(GAME_HTTP_ERROR_TYPE.REQUEST_ERROR);
        }
        request.ontimeout = function () {
            loadingMgr.hideLoading()
            request.abort()
            request = null
            failcb && failcb(GAME_HTTP_ERROR_TYPE.REQUEST_TIMEOUT);
        }
        // var body = "";
        // var data = param || {};

        // //增加排序
        // let sortKey = Object.keys(data).sort()
        // for (var key in sortKey) {
        //     var paramStr = sortKey[key] + "=" + data[sortKey[key]];
        //     if (body == "") {
        //         body += paramStr;
        //     } else {
        //         body += "&" + paramStr;
        //     }
        // }
        logMgr.log(`http post request: ${url}`)
        loadingMgr.showLoading()
        request.send(JSON.stringify(param));
    }
}